//
//  AboutView.m
//  BabyHelper
//
//  Created by  thomas on 11-8-15.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AboutView.h"
#import "Connections.h"
#import "QVerifyWebViewController.h"
#import "BabyHelperAppDelegate.h"
#import "ComposeViewController.h"

@implementation AboutView
@synthesize  aboutFollowButton,aboutRecommendButton,aboutVisitButton;
@synthesize  iconView;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.iconView.image = [UIImage imageNamed:@"icon_about"];
	self.title = NSLocalizedString(@"About", @"");
	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(IBAction)toggleFollowButton:(id)sender{
	BabyHelperAppDelegate *appDelegate = (BabyHelperAppDelegate *)[[UIApplication sharedApplication] delegate];
	UIViewController *controller = [OAuthController controllerToEnterCredentialsWithEngine: appDelegate._engine delegate: self];
	if (controller) 
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"") 
														message:NSLocalizedString(@"You are not log in yet", @"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	else {
		[OAuthEngine setCurrentOAuthEngine:appDelegate._engine];
		WeiboClient *client = [[WeiboClient alloc] initWithTarget:self 
														   engine:[OAuthEngine currentOAuthEngine]
														   action:@selector(followDidSucceed:obj:)];
		//[client follow:sinaWeiBoAccount];
		[client follow:sinaWeiBoAccount];
	}
}

-(IBAction)toggleRecommendButton:(id)sender{
	BabyHelperAppDelegate *appDelegate = (BabyHelperAppDelegate *)[[UIApplication sharedApplication] delegate];
	UIViewController *controller = [OAuthController controllerToEnterCredentialsWithEngine: appDelegate._engine delegate: self];
	if (controller) 
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"") 
														message:NSLocalizedString(@"You are not log in yet", @"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	else {
		[OAuthEngine setCurrentOAuthEngine:appDelegate._engine];
		ComposeViewController *composeViewController = [[ComposeViewController alloc]init];
		[self presentModalViewController:composeViewController animated:YES];
		composeViewController.messageTextField.text = RECOMMEND_TEMPLATE;
		[composeViewController newTweet];					
	}
}

-(IBAction)toggleVisitButton:(id)sender{
	AccessWebSiteView *childView = [[AccessWebSiteView alloc] initWithNibName:@"AccessWebSiteView" bundle:nil];
	[self.navigationController pushViewController:childView animated:YES];
	[childView release];
}


- (void)followDidSucceed:(WeiboClient*)sender obj:(NSObject*)obj;
{	
	 
	 
    if (sender.hasError) {
		if ([sender.errorDetail rangeOfString:@"40303"].location != NSNotFound) {
			//User already follow the iBabyNotes
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"") 
															message:FOLLOW_TEMPLATE
														   delegate:nil 
												  cancelButtonTitle:@"OK"
												  otherButtonTitles:nil];
			[alert show];
			[alert release];
			return;
		}
		else{
		    [sender alert];	
			return;
		}
    }
 	
    NSDictionary *dic = nil;
    if (obj && [obj isKindOfClass:[NSDictionary class]]) {
        dic = (NSDictionary*)obj;    
    }
	
    if (dic) {
        Status* sts = [Status statusWithJsonDictionary:dic];
		if (sts) {
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"") 
															message:FOLLOW_TEMPLATE
														   delegate:nil 
												  cancelButtonTitle:@"OK"
												  otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
    }
}

@end
